package com.ft.oa.service.customer.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.customer.Customer;
import com.ft.oa.domain.customer.CustomerTail;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.customer.CustomerMapper;
import com.ft.oa.mapper.customer.CustomerTailMapper;
import com.ft.oa.service.customer.CustomerTailService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import com.ft.oa.web.customer.rq.CustomerTailPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CustomerTailServiceImpl implements CustomerTailService {

    @Resource
    private CustomerTailMapper customerTailMapper;

    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private DirectoryService directoryService;



    /**
     * 客户跟踪添加
     * @param customerTail
     * @return
     */
    @Override
    public int insertCustomerTail(CustomerTail customerTail) {

        Customer customer = customerMapper.selectById(customerTail.getCustomerId());
        int count =  customer.getRecord();
        List<AttachmentDto> attachments = customerTail.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            customerTail.setAttachmentRel(json);
        }
        int insert = customerTailMapper.insert(customerTail);
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_CUSTOMER_TAIL_FILE.getCode(),String.valueOf(customerTail.getId()),"1","");
        if (insert>=1){
            count++;
            customer.setRecord(count);
            customer.setCreatorTime(customerTail.getMaintenanceData());
            customerMapper.updateById(customer);
            return insert;
        }
        return 0;
    }

    /**
     * 查询客户跟踪列表
     * @return
     */
    public IPage<CustomerTail> selectCustomerTail(CustomerTailPage customerTailPage){
        IPage<CustomerTail> page = new Page<>(customerTailPage.getPage(),customerTailPage.getSize());
        QueryWrapper<CustomerTail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id",customerTailPage.getCustomerId());
        if (customerTailPage.getStartTime()!=null&&customerTailPage.getEndTime()!=null) {
            queryWrapper.ge("maintenance_data", customerTailPage.getStartTime());
            queryWrapper.le("maintenance_data", customerTailPage.getEndTime());
        }
        if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
            queryWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
        }
        IPage<CustomerTail> customerTailPages = customerTailMapper.selectPage(page, queryWrapper);
        customerTailPages.getRecords().forEach( e->{
            if(e.getAttachmentRel() != null){
                e.setAttachments(JSONObject.parseArray(e.getAttachmentRel(), AttachmentDto.class));
            }
        });
        return customerTailPages;
    }


    @Override
    public CustomerTail selectAddress(Integer id) {
        QueryWrapper<CustomerTail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
      return  customerTailMapper.selectOne(queryWrapper);
    }


}
